home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.02 Feb 90 / Mouse Source / TrackPosition.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-22  |  5.7 KB  |  256 lines  |  [TEXT/KAHL]

  1. /*                                        TrackPosition.c                                        */
  2. /*
  3.  * Copyright © 1989 Martin Minow. All rights reserved.
  4.  *
  5.  * Convert from textual to positional coordinates.
  6.  *
  7.  * void
  8.  * TrackGetPosition(dot, track_handle, hpixel, vpixel)
  9.  * DOT                    dot;
  10.  * TrackHandle    track_handle;
  11.  * LONGINT            *hpixel;
  12.  * LONGINT            *vpixel;
  13.  *
  14.  * TrackGetPosition returns the point (in window-local
  15.  * coordinates) of the character at the indicated
  16.  * position.  Note that the results are long integers.
  17.  * You should not assume that the point is visible.
  18.  *
  19.  * LONGINT
  20.  * TrackGetHeight(endline, startline, track_handle)
  21.  * LONGINT            endline;
  22.  * LONGINT            startline;
  23.  * TrackHandle    track_handle;
  24.  *
  25.  * TrackGetHeight returns the total height of all of
  26.  * the lines in the text between and including startline
  27.  * and endline.
  28.  *
  29.  * _Track_dot_to_col() determines the horizontal pixel
  30.  *                coordinate for the specified DOT.
  31.  * _Track_dot_to_bol() locates the pixel coordinates at
  32.  *                the beginning of the line containing DOT.
  33.  * _Track_dot_to_eol() locates the pixel coordinates at
  34.  *                the end of the line containing DOT.
  35.  * _Track_row() determines the row (index into lineStarts)
  36.  *                containing DOT.
  37.  * _Track_row_pixel() determines the vertical location
  38.  *                in window-local coordinates for the given row.
  39.  */
  40. #include "TrackEdit.h"
  41. #define TR (*tr)
  42. static LONGINT    h_pixel(TrackPtr, LONGINT, INTEGER);
  43. static INTEGER    line_length(TrackPtr, LONGINT);
  44.  
  45. /*
  46.  * Return the window-position of dot.
  47.  */
  48. void
  49. TrackGetPoint(dot, track_handle, hpixel, vpixel)
  50. DOT                                dot;
  51. TrackHandle                track_handle;
  52. LONGINT                        *hpixel;
  53. LONGINT                        *vpixel;
  54. {
  55.         register TrackPtr    tr;
  56.         _Track_state            state;
  57.         LONGINT                        row;
  58.         INTEGER                        col;
  59.     
  60.         tr = _Track_lock(track_handle, &state);
  61.         row = _Track_row(tr, dot);
  62.         col = (INTEGER) (dot - TR.lineStarts[row]);
  63.         *hpixel = h_pixel(tr, row, col);
  64.         *vpixel = _Track_row_pixel(tr, row);
  65.         _Track_unlock(&state);
  66. }
  67.  
  68. /*
  69.  * Return the height of the selection (in pixels).
  70.  */
  71. LONGINT
  72. TrackGetHeight(end, start, track_handle)
  73. LONGINT                        end;
  74. LONGINT                        start;
  75. TrackHandle                track_handle;
  76. {
  77.         register TrackPtr    tr;
  78.         _Track_state            state;
  79.         LONGINT                        result;
  80.     
  81.         tr = _Track_lock(track_handle, &state);
  82.         result = _Track_row_pixel(tr, _Track_row(tr, end))
  83.                      - _Track_row_pixel(tr, _Track_row(tr, start));
  84.         _Track_unlock(&state);
  85.         return (result);
  86. }
  87.  
  88. /*
  89.  * _Track_dot_to_col()
  90.  * Compute the horizontal position of the DOT in the
  91.  * window.
  92.  */
  93. LONGINT
  94. _Track_dot_to_col(tr, dot)
  95. register TrackPtr    tr;
  96. DOT                                dot;
  97. {
  98.         LONGINT                row;
  99.         INTEGER                col;
  100.         
  101.         row = _Track_row(tr, dot);
  102.         col = (INTEGER) (dot - TR.lineStarts[row]);
  103.         return (h_pixel(tr, row, col));
  104. }
  105.  
  106. /*
  107.  * _Track_dot_to_bol()
  108.  * Compute the horizontal position of the beginning of
  109.  * the line.
  110.  */
  111. LONGINT
  112. _Track_dot_to_bol(tr, row)
  113. register TrackPtr    tr;
  114. LONGINT                        row;
  115. {
  116.         return (_Track_h_origin(tr, row));
  117. }
  118.  
  119. /*
  120.  * _Track_dot_to_eol()
  121.  * Compute the horizontal position of the end of the line.
  122.  */
  123. LONGINT
  124. _Track_dot_to_eol(tr, row)
  125. register TrackPtr    tr;
  126. LONGINT                        row;
  127. {
  128.         INTEGER                col;
  129.         
  130.         col = line_length(tr, row);
  131.         return (h_pixel(tr, row, col));
  132. }
  133.  
  134. /*
  135.  * _Track_row(tr, dot)
  136.  * Return the row that contains dot.  The ancient and
  137.  * honorable binary-chop table lookup algorithm.
  138.  */
  139. LONGINT
  140. _Track_row(tr, dot)
  141. register TrackPtr    tr;
  142. DOT                                dot;
  143. {
  144.         register LONGINT    mid;
  145.         register LONGINT    low;
  146.         register LONGINT    high;
  147.         
  148.         low = 0;
  149.         high = TR.nLines - 1;
  150.         while (low <= high) {
  151.             mid = low + (high - low) / 2;
  152.             if (dot < TR.lineStarts[mid])
  153.                 high = mid - 1;
  154.             else if (dot >= TR.lineStarts[mid + 1])
  155.                 low = mid + 1;
  156.             else {
  157.                 return (mid);
  158.             }
  159.         }
  160.         /*
  161.          * Don't return beyond the last value.
  162.          */
  163.         if (low >= TR.nLines && TR.nLines > 0)
  164.             low = TR.nLines - 1;
  165.         return (low);
  166. }
  167.  
  168. LONGINT
  169. _Track_row_pixel(tr, row)
  170. register TrackPtr    tr;
  171. LONGINT                        row;
  172. {
  173.         return (
  174.                 (row * TR.lineHeight)
  175.             + TR.viewRect.top                /* To window-local space    */
  176.             + TR.fontAscent                    /* To character origin        */
  177.             - TR.topPixel                        /* Offset by scrolling        */
  178.         );
  179. }
  180.  
  181. /*
  182.  * h_pixel()
  183.  * Compute the horizontal pixel position in window-local
  184.  * coordinates of the specified [row, col].
  185.  */
  186. LONGINT
  187. h_pixel(tr, row, col)
  188. register TrackPtr    tr;
  189. LONGINT                        row;
  190. INTEGER                        col;
  191. {
  192.         LONGINT                pixel;
  193.         Ptr                        line_start;
  194.         INTEGER                state;
  195.  
  196.         state = HGetState(TR.hText);
  197.         HLock(TR.hText);
  198.         line_start = (*TR.hText) + TR.lineStarts[row];
  199.         pixel = TextWidth(line_start, 0, col)
  200.                     + _Track_h_origin(tr, row);
  201.         HSetState(TR.hText, state);
  202.         return (pixel);
  203. }
  204.  
  205. /*
  206.  * _Track_h_origin()
  207.  * Compute the horizontal pixel position in window-local
  208.  * coordinates of the first (i.e., leftmost) character
  209.  * in this row.  This is the only function that
  210.  * knows about text justification.
  211.  */
  212. LONGINT
  213. _Track_h_origin(tr, row)
  214. register TrackPtr    tr;
  215. LONGINT                        row;
  216. {
  217.         LONGINT                pixel;
  218.         INTEGER                window_width;
  219.         INTEGER                text_width;
  220.         INTEGER                state;
  221.         
  222.         pixel = TR.viewRect.left - TR.leftPixel;
  223.         if (TR.just != 0) {                    /* Not left justified?    */
  224.             window_width = TR.viewRect.right - TR.viewRect.left;
  225.             state = HGetState(TR.hText);
  226.             HLock(TR.hText);
  227.             text_width = TextWidth(
  228.                                         (*TR.hText) + TR.lineStarts[row],
  229.                                         0,
  230.                                         line_length(tr, row)
  231.                                     );
  232.             HSetState(TR.hText, state);
  233.             if (TR.just < 0)                    /* Right justified            */
  234.                 pixel += (window_width - text_width);
  235.             else {                                        /* Center justified            */
  236.                 pixel += ((window_width - text_width) / 2);
  237.             }
  238.         }
  239.         return (pixel);
  240. }
  241.  
  242. /*
  243.  * line_length(tr, row)
  244.  * Return the number of characters in the specified row.
  245.  */
  246. static int
  247. line_length(tr, row)
  248. register TrackPtr    tr;
  249. LONGINT                        row;
  250. {
  251.         if (row >= TR.nLines)
  252.             return (0);
  253.         return (TR.lineStarts[row + 1] - TR.lineStarts[row]);
  254. }
  255.  
  256.